Page({

  /**页面的初始数据 */
  	data: {

    addressList:[]

  },

  /*生命周期函数--监听页面加载*/

  onLoad: function (options) {
    var token = wx.getStorageSync('token');
    var user = wx.getStorageSync('user');
    var that = this;
    wx.request({
      url: 'http://127.0.0.1:8080/system/address/list?userId='+user.userId, // 获取地址列表接口
      method: 'get',
      header: {
        'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错
        'Authorization': token
      },
      success(res) {
        if (res.statusCode==200) {
          that.setData({
            addressList:res.data.rows
          })
        }
      }
    })

  },

  

  /**生命周期函数--监听页面显示 */
	  onShow: function () {

    this.onLoad();

  },

  addAddress:function(){

    wx.navigateTo({ url: '../addAddress/index' });

  },

  /* 删除item */

  delAddress: function (e) {
    var id = e.target.dataset.id
    var token = wx.getStorageSync('token');
    wx.request({
      url: 'http://127.0.0.1:8080/system/address/'+id, // 删除地址接口
      method: 'delete',
      header: {
        'content-type': 'application/json',  //这里注意POST请求content-type是小写，大写会报错
        'Authorization': token
      },
      success(res) {
        console.log(res)
        if (res.statusCode==200) {
          wx.showToast({
            title: "删除成功",
            icon: 'none',
          })
        }
      }
    })

this.data.addressList.splice(e.target.id.substring(3), 1);

// 更新data数据对象  

if (this.data.addressList.length > 0) {

  this.setData({

    addressList: this.data.addressList

  })
  wx.setStorageSync('addressList', this.data.addressList);

} else {

  this.setData({

    addressList: this.data.addressList

  })
  wx.setStorageSync('addressList', []);

}
  }
})